//
//  UIView+RedPoint.m
//  KidReading
//
//  Created by YangShuai on 16/11/10.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "UIView+RedPoint.h"
#import "ConstDefine.h"
@implementation UIView (RedPoint)

-(UIView*)addRedPointWithSize:(CGFloat)size Offset:(CGPoint)offset{
    UIView* redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    redView.layer.cornerRadius = size / 2;
    redView.layer.masksToBounds = YES;
    [self addSubview:redView];
    [redView JRMAS_makeConstraints:^(JRMASConstraintMaker *make) {
        make.size.JRMAS_equalTo(CGSizeMake(size, size));
        make.top.JRMAS_equalTo(offset.x);
        make.right.JRMAS_equalTo(offset.y);
    }];
    return redView;
}

- (UILabel *)addUnreadCountWithSuperView:(UIView *)superView count:(NSInteger)count offset:(CGPoint)offset{
    if (!count) {
        return nil;
    }
    __weak UIView *wself = self;
    CGFloat length = 32.0/[DataProcessing shareSingle].ratio_h;
    CGFloat font = 20.0/[DataProcessing shareSingle].ratio_h;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    [superView addSubview:label];
    [superView insertSubview:label aboveSubview:self];
    [label JRMAS_makeConstraints:^(JRMASConstraintMaker *make) {
        make.size.JRMAS_equalTo(CGSizeMake(length, length));
        make.right.equalTo(wself.JRMAS_right).offset(-offset.x);
        make.top.equalTo(wself.JRMAS_top).offset(-(offset.y));
    }];
    label.backgroundColor = [UIColor redColor];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:font];
    label.textColor = [UIColor whiteColor];
    NSString *countStr = [NSString stringWithFormat:@"%ld",count];
    if (count > 9) {
        countStr = @"9+";
    }
    label.text = countStr;
    label.layer.cornerRadius = length/2.0;
    label.layer.masksToBounds = YES;
    return label;
}

-(void)viewRotationStart{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 3;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = 1000;
    [self.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
}
-(void)viewRotationStop{
    //取得指定图层动画的媒体时间，后面参数用于指定子图层，这里不需要
    CFTimeInterval interval=[self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    //设置时间偏移量，保证暂停时停留在旋转的位置
    [self.layer setTimeOffset:interval];
    //速度设置为0，暂停动画
    self.layer.speed=0;
}

@end
